package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.book.model.entity.Banner;
import com.book.mapper.BannerMapper;
import com.book.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import java.util.Date;
import java.util.List;

/**
 * <p>
 * 轮播图 服务实现类
 * </p>
 *
 * @author auto
 * @since 2025-04-03 10:33:27
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public List<Banner> getBannerList() {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getEnable, 1);
        return this.list(queryWrapper);
    }

    @Override
    public boolean saveBanner(String banner) {
        Banner banner1 = new Banner();
        banner1.setBannerUrl(banner);
        banner1.setEnable(1);
        banner1.setCreateTime(new Date());
        return this.save(banner1);
    }

    @Override
    public Page<Banner> getBannerImagByPage(Integer page, Integer size) {
        Page<Banner> bannerPage = new Page<>(page, size);
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Banner::getCreateTime);
        Page<Banner> resultPage = this.page(bannerPage, queryWrapper);
        return resultPage;
    }

    @Override
    public boolean enableBanner(String id) {
        Banner banner = this.getById(id);
        banner.setEnable(1);
        return this.updateById(banner);
    }

    @Override
    public boolean disableBanner(String id) {
        Banner banner = this.getById(id);
        banner.setEnable(0);
        return this.updateById(banner);
    }
}
